﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using TBFramework;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Video;
using JetBrains.Annotations;

[ExecuteAlways] 
public class StepProperty : StandardProperty
{
    public CommonUtils commonUtils;
    public StepCurrentPropertyItem stepCurrentProperty = new StepCurrentPropertyItem();
    public List<StepCurrentProperty> stepCurrentProperties = new List<StepCurrentProperty>();
    /// <summary>
    /// 对传入的 StepCurrentProperty 对象进行深拷贝操作。
    /// 深拷贝会创建一个新的 StepCurrentProperty 实例，并将传入对象的属性值复制到新实例中。
    /// </summary>
    /// <param name="property">需要进行深拷贝的 StepCurrentProperty 对象。</param>
    /// <returns>返回一个新的 StepCurrentProperty 对象，其属性值与传入对象相同。</returns>
    public T DeepCopy<T>(T property)
    {

        string json = JsonUtility.ToJson(property);
        //print(json);
        // 从 JSON 字符串反序列化为新对象
        return JsonUtility.FromJson<T>(json);
        // StepCurrentProperty copy = new StepCurrentProperty()
        // {
        //     step1 = property.step1,
        //     step2 = property.step2
        // };
        // return copy;
    }
    [SerializeField] private Default _defaultValue;
    public Default DefaultValue
    {
        get { return _defaultValue; }
        set
        {
            // if (_defaultValue != value)
            // {
            print($"DefaultValue 从 {_defaultValue} 变更为 {value}");
            // 更新默认值
            Init(value);
            _defaultValue = value;
            // 这里可以添加更多自定义逻辑
            //  }
        }
    }

    public void Init(Default defaultValue)
    {
        switch (defaultValue)
        {
            case Default.LEVEL1:
                stepCurrentProperty.stepCurrentProperty = DeepCopy(stepCurrentProperties[0]);

                DefaultStanderdClass defaultStanderdClass = new DefaultStanderdClass();
                new StepClass(this).init(defaultStanderdClass);
                defaultStanderdClass.step1Default();
                break;
            case Default.LEVEL2:
                stepCurrentProperty.stepCurrentProperty = DeepCopy(stepCurrentProperties[1]);
                break;
            case Default.LEVEL3:
                stepCurrentProperty.stepCurrentProperty = DeepCopy(stepCurrentProperties[0]);
                break;
            case Default.LEVEL4:
                stepCurrentProperty.stepCurrentProperty = DeepCopy(stepCurrentProperties[0]);
                break;
        }
    }

    public void PrintMethod(string str)
    {
        print(str);
    }

    Hashtable hashtable = new Hashtable();
    public void Start()
    {

       // string json = "{\"step1\": {\"value1\": 335.0,\"OnceMessagePanel\": false,\"waitTime\": 0.0,\"g\": [{\"instanceID\": 16972},{\"instanceID\": 16912}]},\"step2\": {\"target\": {\"instanceID\": 0},\"targetPos\": {\"x\": 20.0,\"y\": 50.0,\"z\":20.0},\"targetRot\": {\"x\": 20.0,\"y\": 20.0,\"z\": 20.0}}}";
       // stepCurrentProperty = JsonUtility.FromJson<StepCurrentProperty>(json);



      //  hashtable[KeyCode.W] =new DelegateAction(testKeyDown) ;
      //  hashtable[KeyCode.S] = new DelegateAction(testKeyDown) ;

      //  hashtable[KeyCode.A] = new DelegateAction(testKeyDown1) ;
      //  hashtable[KeyCode.D] = new DelegateAction(testKeyDown1) ;
        
      //  hashtable[KeyCode.C] = new DelegateAction(testKeyDown2) ;
      //  hashtable[KeyCode.R] = new DelegateAction(testKeyDown2) ;
    }
    // 修改委托定义，明确参数类型
    // public delegate void DelegateAction(KeyCode keyCode);
    // public DelegateAction daction;
    // public void testKeyDown(KeyCode keyCode)
    // {
    //     if (keyCode == KeyCode.W)
    //     {
    //         Debug.Log("w");
    //     }
    //     if (keyCode == KeyCode.S)
    //     { 
    //         Debug.Log("s");
    //     }
    // }
    // public void testKeyDown1(KeyCode keyCode)
    // {        
    //     if (keyCode == KeyCode.A)
    //     {
    //         Debug.Log("a");
    //     }
    //     if (keyCode == KeyCode.D)
    //     { 
    //         Debug.Log("d");
    //     }
    // }
    // public void testKeyDown2(KeyCode keyCode)
    // {        
    //     if (keyCode == KeyCode.C)
    //     {
    //         Debug.Log("c");
    //     }
    //     if (keyCode == KeyCode.R)
    //     { 
    //         Debug.Log("r");
    //     }
    // }

    // void Update()
    // {
    //     IDictionaryEnumerator _keyide = hashtable.GetEnumerator();
    //     while (_keyide.MoveNext())
    //     { 
    //         if (Input.GetKeyDown((KeyCode)_keyide.Key))
    //         {
    //             // 调用对应的委托方法
    //             DelegateAction action = _keyide.Value as DelegateAction;
    //             action?.Invoke((KeyCode)_keyide.Key);
    //         }

    //     }
    // }
}




// 运行时动态调用


public class StepClass
{
    public StepProperty thisStepProperty;
    public StepClass(StepProperty stepProperty)
    {
       thisStepProperty = stepProperty;
    }

    public void init(DefaultStanderdClass sensor)
    {
        sensor.step1Default = () =>
        {          
            DynamicExecutor<StepProperty> d = new DynamicExecutor<StepProperty>();
            Debug.Log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
            d.Run(thisStepProperty,"PrintMethod","tangbo");
        };
    }
}



[System.Serializable]
public enum Default
{
    LEVEL1,
    LEVEL2,
    LEVEL3,
    LEVEL4
}

[System.Serializable]
public class CommonUtils
{
    public GameObject movie;
    public bool isOver = false;

   
}

public class DefaultStanderdClass
{
    public Action step1Default;    
}

[System.Serializable]
public class StepCurrentPropertyItem
{
    public int Score;
    public Movie movie;
    public Tools tools;
    public CarouselImageObj carouselImageObj;
    public MultipleChoiceQuestionObj multipleChoiceQuestionObj;
    public PromptInformationObj promptInformationObj;
    public PictureOptionsObj pictureOptionsObj;

    [SerializeField]
    private bool _skip;    
    public bool skip
    {
        get
        {
            bool currentValue = _skip;
            _skip = false; 
            return currentValue;
        }
        set
        {
            _skip = value;
        }
    }

    public StepCurrentProperty stepCurrentProperty = new StepCurrentProperty();
}

[System.Serializable]
public class StepCurrentProperty
{
    [Tooltip("关卡名称")]
    public string levelName;
    [Tooltip("轮播图图片与判定")]
    public List<CarouselImage> carouselImage = new List<CarouselImage>();
    [Tooltip("关卡保存值")]
    public LevelScore levelScore;
    [Tooltip("工具选择的判定的值")]
    public List<ScoreDetermination> toolsChangeValue = new List<ScoreDetermination>();
    [Tooltip("多选单选问题")]
    public List<MultipleChoiceQuestion> multipleChoiceQuestion = new List<MultipleChoiceQuestion>();

    [Tooltip("图片选项")]
    public List<PictureOptions> pictureOptions = new List<PictureOptions>();

    private Dictionary<string, object> dynamicProperties = new Dictionary<string, object>();
    // 动态添加属性
    public void AddProperty(string propertyName, object value)
    {
        if (!dynamicProperties.ContainsKey(propertyName))
        {
            dynamicProperties.Add(propertyName, value);
        }
        else
        {
            dynamicProperties[propertyName] = value;
        }
    }
    // 获取动态属性值
    public object GetProperty(string propertyName)
    {
        if (dynamicProperties.ContainsKey(propertyName))
        {
            return dynamicProperties[propertyName];
        }
        return null;
    }
}
/// <summary>
/// 视频播放结构体
/// </summary>
[System.Serializable]
public struct Movie
{
    public VideoClip videoClip;
    public VideoPlayer videoPlayer;
}
/// <summary>
/// 轮播图片结构体
/// </summary>
[System.Serializable]
public class CarouselImage
{
    // 图片按钮
    // public int isClickDis;
    // 图片列表
    public Sprite[] t2d;
    // 图片显示组件
    //  public Image[] rawImage;
    // 轮播图按钮
    //  public Button[] buttons;
    // 当前页码
    //  public int currentPageNum;
    // 是否正在点击
    //  public bool isButtonClick;
    // 轮播图翻页数
    //   public int turningPageNum;
    // 是否翻页结束
    public bool isOver;
}

[System.Serializable]
public class PictureOptions
{
    public Dictionary<string, string> formKeyValues = new Dictionary<string, string>();
    public Sprite[] t2d;
    public Sprite d2d;
    public bool isOver;
}




[System.Serializable]
public class Tools
{
    public GameObject toolsUIParent;
    //工具选择的ID
    [HideInInspector]
    public string toolId;
    //工具选择的名称
    [HideInInspector]
    public string buttonName;
    [HideInInspector]
    //工具选择的图片。
    public Sprite sprite;
}
/// <summary>
/// 轮播图
/// </summary>
[System.Serializable]
public class CarouselImageObj
{ 
    [Tooltip("轮播图翻页效果")]
    public GameObject CarouselImageUIParent;
}

/// <summary>
/// 关卡记录 数据全局方法
/// </summary>
[System.Serializable]
public class LevelScore
{ 
    public List<LevelClass> levelList = new List<LevelClass>();
}


/// <summary>
/// 选择判定
/// </summary>
[System.Serializable]
public class ScoreDetermination
{
    //单个选择第一个值
    [HideInInspector]
    public string selectValue;
    //单个选择第二个值
    [HideInInspector]
    public string selectValue1;
    //单个设定第一个值
    public string changeValue;
    //单个设定第二个值
    public string changeValue1;
    public List<ValueListClass> ValueList = new List<ValueListClass>();
    public List<(int row, int col, string value)> selectValueListObj = new List<(int row, int col, string value)>();
    public List<(int row, int col, string value, float calculationError)> changeValueListObj = new List<(int row, int col, string value, float calculationError)>();    
}
[System.Serializable]
public struct ValueListClass
{
    [Tooltip("行")]
    public int row;
    [Tooltip("列")]
    public int col;
    [Tooltip("值")]
    public string answer;
    [Tooltip("输入值")]
    public string inputValue;
    [Tooltip("误差")]
    public float calculationError;
    [Tooltip("是否正确")]
    public bool isCorrect;
    [Tooltip("输入框")]
    public InputField inputField;
    [Tooltip("按钮")]
    public Button button;
}
//记录某一关记录的值
[System.Serializable]
public struct LevelClass
{
    [Tooltip("关卡")]
    public int level;
    [Tooltip("值")]
    public string value;
    [Tooltip("备注")]
    public string remark;
}

[System.Serializable]
public struct MultipleChoiceQuestionObj
{ 
    public GameObject singleChoiceQuestionUIParent;
    public GameObject multipleChoiceQuestionUIParent;
}

[System.Serializable]
public struct PromptInformationObj
{
    public GameObject promptInformationUIParent;
}

[System.Serializable]
public struct PictureOptionsObj
{
    public GameObject PictureOptionsObjUIParent;
}


public enum QuestionType
{
    SingleChoice,
    MultipleChoice
}

[System.Serializable]
public class MultipleChoiceQuestion
{
    [Tooltip("问题")]
    public string question;
    [Tooltip("选项")]
    public List<string> options;
    [HideInInspector]
    public string selectAnswer;
    [Tooltip("正确答案")]
    public string correctAnswer;
    [Tooltip("问题类型")]
    public QuestionType questionType;
}